package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.mongo.Visitors;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Long> relativeLike(Long userId, Integer page, Integer pagesize, Integer type, String nickname) {
        //拿到自己喜欢列表
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        //提取喜欢人id
        List<Long> collect = userLikes.stream().map(c -> {
            return c.getLikeUserId();
        }).collect(Collectors.toList());
        //
        Query query1 = Query.query(Criteria.where("userId").in(collect).and("likeUserId").is(userId).and("isLike").is(true));
        //long count1 = mongoTemplate.count(query1, UserLike.class);
        List<UserLike> userLikes1 = mongoTemplate.find(query1, UserLike.class);
        List<Long> collect1 = userLikes1.stream().map(c -> {
            return c.getUserId();
        }).collect(Collectors.toList());
        return collect1;
    }

    @Override
    public List<Long> likeMeList(Long userId, Integer page, Integer pagesize, Integer type, String nickname) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true))
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIds = userLikes.stream().map(c -> {
            return c.getUserId();
        }).collect(Collectors.toList());
        return likeUserIds;
    }

    @Override
    public List userLike2(Long id, Long userId) {
        Query query = Query.query(Criteria.where("id").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    @Override
    public void addLike(Long userId, Long likeUserId) {
        UserLike userLike = new UserLike();
        userLike.setLikeUserId(likeUserId);
        userLike.setUserId(userId);
        userLike.setIsLike(true);
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUpdated(System.currentTimeMillis());
        mongoTemplate.save(userLike);
    }

    @Override
    public boolean ifLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(likeUserId)
                .and("isLike").is(true)
                .and("likeUserId").is(userId));
        UserLike one = mongoTemplate.findOne(query, UserLike.class);
        if (one == null) {
            return false;
        }
        return true;
    }

    @Override
    public void deleLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("likeUserId").is(likeUserId)
                .and("userId").is(userId)
                .and("isLike").is(true));
        mongoTemplate.remove(query,UserLike.class);
    }

    @Override
    public void delefriend(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        mongoTemplate.remove(query, Friend.class);
    }

    @Override
    public List meLike(Long userId, Integer page, Integer pagesize, Integer type, String nickname) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true))
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIds = userLikes.stream().map(c -> {
            return c.getLikeUserId();
        }).collect(Collectors.toList());
        return likeUserIds;
    }

    //来访人员详细信息
    @Override
    public List<Long> visitors(Long userId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId))
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        List<Visitors> visitors = mongoTemplate.find(query, Visitors.class);
        List<Long> collect = visitors.stream().map(Visitors::getVisitorUserId).collect(Collectors.toList());
        return collect;
    }
}
